<?php
/*
Plugin Name: Urban Meandering Events
Plugin URI: http://urban-meandering-local.com/
Description: A plugin that will create a widget that will show upcoming events
Version: 1.0
Author: Abraham B. Basbas
Author URI: 
License: GPL2

*/

require_once('events.class.php');
require_once('events.widgets.class.php');

// register post type

function register_events_posts()
{
    $post_type = 'um_events';
    
    $labels = array(
    'name' => _x('Events', 'post type general name'),
    'singular_name' => _x('Events', 'post type singular name'),
    'add_new' => _x('Add New', 'events'),
    'add_new_item' => __('Add New Events'),
    'edit_item' => __('Edit Events'),
    'new_item' => __('New Events'),
    'all_items' => __('All Events'),
    'view_item' => __('View Events'),
    'search_items' => __('Search Events'),
    'not_found' =>  __('No events found'),
    'not_found_in_trash' => __('No events found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => __('Events')

  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
      'taxonomies' => array(
										'category',
										'post_tag'
							)
  ); 
 
  
    register_post_type($post_type, $args);
}

add_action( 'init', 'register_events_posts' );

register_activation_hook( __FILE__, 'um_events_activate' );

function um_events_activate(){
    
    
    um_default_options();
}

function um_default_options(){
    $options = array('numberofposts' => 10,
                     'show_events_before' => 13 );
    
    add_option('um_events_defaults', serialize($options));
}


add_action( 'add_meta_boxes', 'add_um_events_metabox' );

//add meta box
function add_um_events_metabox()
{
      add_meta_box( 
        'um_events_id',
        __( 'Event Date', 'events' ),
        'create_um_events_metabox',
        'um_events',
        'side',
        'high'
    );
}

add_action('admin_enqueue_scripts','enqueue_datepicker');

function enqueue_datepicker($hook)
{
    wp_enqueue_script('jquery-ui-datepicker');
    wp_enqueue_style('jquery-ui-css',  '/'. PLUGINDIR .'/um_events/css/jquery-ui-1.8.16.custom.css','1.0');
}
/* Prints the box content */
function create_um_events_metabox( $post ) { 
//start date - end date
    //$details = unserialize(get_post_meta($post->ID,'um_events_details',true));
   
    $start_date = get_post_meta($post->ID,'um_events_start_date',true);
    $end_date   = get_post_meta($post->ID,'um_events_end_date',true);
    $location   = get_post_meta($post->ID,'um_events_location',true);
    
    
    wp_nonce_field( plugin_basename( __FILE__ ), 'um_events_noncename' );
    echo '<label fore=""> ';
    _e('Start Date:','events');
    echo '</label>';
    echo '<input type="text" id="um_events_start_date" name="um_events_start_date" value="'. $start_date .'" size="30" style="width:97%" />';
     ?>
                <script type="text/javascript">
              jQuery(document).ready(function($) {
                var dateField = "um_events_start_date";
                $('#'+dateField).datepicker({
                  minDate: -0,
                  dateFormat: 'yy-mm-dd'
                });
              });
            </script>
<?php
    echo '<br />';
    echo '<label for=""> ';
    _e('End Date:','events');
    echo '</label>';
    echo '<input type="text" id="um_events_end_date" name="um_events_end_date" value="'. $end_date .'" size="30" style="width:97%" />';
  ?>
                <script type="text/javascript">
              jQuery(document).ready(function($) {
                var dateField = "um_events_end_date";
                $('#'+dateField).datepicker({
                  minDate: -0,
                  dateFormat: 'yy-mm-dd'
                });
              });
            </script>
<?php   
   
    echo '<br />';
    echo '<label for=""> ';
    _e('Location:','events');
    echo '</label>';
    echo '<textarea name="um_events_location" style="width:97%">' . $location . '</textarea>';
    

//location
}


/* When the post is saved, saves our custom data */
function um_events_save_postdata( $post_id ) {  
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( isset($_POST['um_events_noncename']) ? $_POST['um_events_noncename'] : '', plugin_basename( __FILE__ ) ) )
      return;

  
  // Check permissions
  if ( 'page' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }
  
  
//
//  // OK, we're authenticated: we need to find and save the data
//
  $start_date = $_POST['um_events_start_date'];
  $end_date = $_POST['um_events_end_date'];
  $location = $_POST['um_events_location'];

//  $args = array('start_date' => $start_date,
//                  'end_date' => $end_date,
//                  'location' => $location);
//  
    //update_post_meta($post_id, 'um_events_details', serialize($args));
    update_post_meta($post_id, 'um_events_start_date', $start_date);
    update_post_meta($post_id, 'um_events_end_date',$end_date);
    update_post_meta($post_id, 'um_events_location', $location);

}

add_action( 'save_post', 'um_events_save_postdata' );

function get_um_events_options()
{
    $data = get_option('um_events_defaults');
    
   if (is_serialized($data))
    $data = unserialize($data);

   return (object) $data;
}
